package com.lxm.config.canal;

import com.lxm.common.contants.BlogConstants;
import com.lxm.entity.BlogType;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

@Component
@CanalTable("blog_type")
@Slf4j
public class BlogTypeCanal implements EntryHandler<BlogType> {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void insert(BlogType blogType) {
        log.debug("新增博客类型：" + blogType);
        stringRedisTemplate.opsForHash().put(BlogConstants.BLOG_TYPE_KEY,
                blogType.getId().toString(), blogType.getName());
    }

    @Override
    public void update(BlogType before, BlogType after) {
        stringRedisTemplate.opsForHash().put(BlogConstants.BLOG_TYPE_KEY,
                after.getId().toString(), after.getName());
    }

    @Override
    public void delete(BlogType blogType) {
        stringRedisTemplate.opsForHash().delete(BlogConstants.BLOG_TYPE_KEY,
                blogType.getId().toString());
    }
}
